package com.fsu.database.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.fsu.database.dao.Book;
import com.fsu.database.dao.DVD;
import com.fsu.database.dao.DatabaseManager;
import com.fsu.database.dao.Media;

@SuppressWarnings("serial")
public class MainFrame extends JFrame{

	public MainFrame(boolean isAdmin) {
		super("COMP4400");
		init(isAdmin);
		
	}
	
	private void init(boolean isAdmin) {
		//Toolkit tk = Toolkit.getDefaultToolkit();
		//Dimension size = tk.getScreenSize();
		
		setSize(800, 600);
		setupControls(isAdmin);
		setVisible(true);
	}
	
	private void setupControls(boolean isAdmin) {
		
		final JPanel searchPanel = new JPanel();
		final JPanel adminPanel = new JPanel();
		
		
		final JTextField searchTf = new JTextField(20);
		final JButton searchBtn = new JButton("Search");
		final JComboBox<String> filterCb = new JComboBox<String>(new String[] { "All", "Books", "DVDs" });
		final JTable resultsTbl = createTable();
		final JButton adminBtn = new JButton("Admin Control Panel");
		
		final JScrollPane sp = new JScrollPane(resultsTbl);
		
		searchBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = searchTf.getText();
				String filter = (String)filterCb.getSelectedItem();
				
				try {
		
					if (filter.equals("Books")) {
						 List<Book> books = DatabaseManager.searchBooks(text, null);
						 fillTable(resultsTbl, books, "books");
					} else if (filter.equals("DVDs")) {
						List<DVD> dvds = DatabaseManager.searchDvds(text, null);
						fillTable(resultsTbl, dvds, "dvds");
					} else {
						List<Media> media = DatabaseManager.searchMedia(text, null);
						fillTable(resultsTbl, media, "media");
					}
				}
				catch(SQLException ex) {
					JOptionPane.showMessageDialog(MainFrame.this, "SQL Error " + ex.getErrorCode() + ":" + ex.getMessage());
					ex.printStackTrace();
				}
			}
			
		});
		
		adminBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new AdminPanel(MainFrame.this);
			}
			
		});
		
		searchPanel.add(filterCb);
		searchPanel.add(searchTf);
		searchPanel.add(searchBtn);
		
		adminPanel.add(adminBtn);
		
		add(searchPanel, BorderLayout.NORTH);
		//add(resultsTbl, BorderLayout.CENTER);
		add(sp, BorderLayout.CENTER);
		
		if (isAdmin) {
			add(adminPanel, BorderLayout.SOUTH);
		}
	}
	
	private JTable createTable() {
		Vector<Vector<String>> data = new Vector<Vector<String>>();
		Vector<String> colNames = new Vector<String>();
		colNames.add("Title");
		colNames.add("Year");
		colNames.add("Price");
		
		return new JTable(new DefaultTableModel(data, colNames));
	}
	
	private void fillTable(JTable table, List<? extends Media> media, String type) {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		model.setRowCount(0);
		for(Media m : media) {
			Vector<String> row = new Vector<String>();
			Vector<String> colNames = new Vector<String>();
			if(type.equalsIgnoreCase("books")) {
				Book b = (Book) m;
				colNames.add("Title");
				colNames.add("Year");
				colNames.add("Price");
				colNames.add("ISBN");
				colNames.add("Subject");
				colNames.add("Publisher");
				colNames.add("Authors");
					
				row.add(b.getTitle());
				row.add(Integer.toString(b.getYear()));
				row.add(Float.toString(b.getPrice()));
				row.add(b.getISBN());
				row.add(b.getSubject());
				row.add(b.getPublisher().toString());
				row.add(b.getAuthors().toString());
				
			} else if (type.equalsIgnoreCase("dvds")) {
				DVD dvd = (DVD) m;
				colNames.add("Title");
				colNames.add("Year");
				colNames.add("Price");
				colNames.add("Director");
				colNames.add("Genre");
				colNames.add("Cast");
		
				row.add(dvd.getTitle());
				row.add(Integer.toString(dvd.getYear()));
				row.add(Float.toString(dvd.getPrice()));
				row.add(dvd.getDirector());
				row.add(dvd.getGenre());
				row.add(dvd.getCast().toString());
				
			} else {
				colNames.add("Title");
				colNames.add("Year");
				colNames.add("Price");
				
				row.add(m.getTitle());
				row.add(Integer.toString(m.getYear()));
				row.add(Float.toString(m.getPrice()));
			}
			
			model.setColumnIdentifiers(colNames);
			model.addRow(row);
		}
	}
}
